This example mirrors brainvoyager.html.

In [1]:
from pathlib import Path

from ipyniivue import download_dataset

BASE_API_URL = "https://niivue.com/demos/images/"
DATA_FOLDER = Path("images")

download_dataset(
    BASE_API_URL,
    DATA_FOLDER,
    files=[
        "sub-test02_left_hemisphere_4_curvature_maps.smp.gz",
        "sub-test02_left_hemisphere.srf.gz",
    ],
)
sub-test02_left_hemisphere_4_curvature_maps.smp.gz already exists.
sub-test02_left_hemisphere.srf.gz already exists.
Dataset downloaded successfully to images.
In [2]:
import ipywidgets
from IPython.display import display

import ipyniivue

# 1. Initialize NiiVue
nv = ipyniivue.NiiVue()
nv.opts.show_3d_crosshair = True
nv.opts.back_color = [0, 0, 0, 1]

# 2. Set Slice Type to Render
nv.set_slice_type(ipyniivue.SliceType.RENDER)

# 3. Load Mesh with a Layer
mesh_layer = {
    "path": DATA_FOLDER / "sub-test02_left_hemisphere_4_curvature_maps.smp.gz",
    "colormap": "rocket",
    "cal_min": 0.0,
    "cal_max": 0.5,
    "opacity": 0.7,
}

nv.load_meshes(
    [
        {
            "path": DATA_FOLDER / "sub-test02_left_hemisphere.srf.gz",
            "rgba255": [255, 255, 255, 255],
            "layers": [mesh_layer],
        }
    ]
)

# 4. Set Initial Clip Plane
nv.set_clip_plane(-0.1, 270, 0)

# 5. Create Interface Controls using ipywidgets

# Timepoint Slider
time_slider = ipywidgets.IntSlider(min=0, max=3, value=0, description="Timepoint")


def on_time_change(change):
    """Handle time change."""
    if nv.meshes:
        nv.meshes[0].layers[0].frame_4d = change["new"]


time_slider.observe(on_time_change, names="value")

# Opacity Slider
opacity_slider = ipywidgets.FloatSlider(
    min=0.1, max=1.0, value=0.7, step=0.1, description="Opacity"
)


def on_opacity_change(change):
    """Handle opacity change."""
    if nv.meshes:
        nv.meshes[0].layers[0].opacity = change["new"]


opacity_slider.observe(on_opacity_change, names="value")

# Shader Selection (Dropdown)
shader_dropdown = ipywidgets.Dropdown(
    options=nv.mesh_shader_names(), value="Phong", description="Shader"
)


def on_shader_change(change):
    """Handle shader change."""
    if nv.meshes:
        nv.set_mesh_shader(nv.meshes[0].id, change["new"])


shader_dropdown.observe(on_shader_change, names="value")

# 6. Layout and Display
controls = ipywidgets.VBox([time_slider, opacity_slider, shader_dropdown])
display(controls)
display(nv)